<?php 
header('Content-type: application/json');

try {
    require ('connect.php');
    require ('function.php');

	//■画面の初期表示
	if($_GET['text'] == "init"){
		drawTodoTable();
		
//		   $f = fopen('log1.txt', 'w');
 //   fwrite($f, "hello");
  //  fclose($f);
		
	}
    
    //■操作が削除の場合
    if ($_GET['text'] == "delete") {
        $id = $_GET['id'];
        $sql = "delete from expose where id = '".$id."'";
        //$sql = "delete from expose where id = '20'" ;

		
		$result = mysql_query($sql);
		//メインテーブルの描画
		drawTodoTable();
    }
    // ■操作が「登録」の場合
    elseif ($_GET["text"] == "create") {
        $sql = "insert  into expose (content,jissai,yosou,bikou) values ('"
		. $_GET['exposeContent'] . "','"
		. $_GET['jissai']  . "','"
		. $_GET['yosou']  . "','"
		. $_GET['bikou'] . "')";
		
        $f = fopen('log1.txt', 'w');
        fwrite($f, "$sql");
        fclose($f);
		
		$result = mysql_query($sql);
		//メインテーブルの描画
		drawTodoTable();
    } 
	//　■ダイスをフル（未実装）
	elseif ($_GET {"text" == "dice"} ) {
    }
    //■操作が「完了」の場合
    elseif ($_GET['text'] == "fin") {
        $id = $_GET['id'];
        $sql = "update expose set dt_fin =current_date,"."dt_finTime = current_time"."  where id ='".$id."'";
        $result = mysql_query($sql);
        
        $f = fopen('log1.txt', 'a+');
        fwrite($f, "¥r log = " . $sql);
        fclose($f);
      
    } elseif ($_GET['text'] == 'showPastData') {
    	  //■操作が「過去データの表示」	
        $sql = "select * from expose where dt_fin <> '0000-00-00'";
        
        $result = mysql_query($sql);
        
        while ($row = mysql_fetch_assoc($result)) {
            $result_array[] = $row;
        }
        echo json_encode($result_array);
        exit;

    
        //■操作が「保留」の場合
    } elseif ($_GET['text'] == 'pending') {
        $id = $_GET['id'];
        $sql = "update expose set isPending = 1"."  where id ='".$id."'";
        $result = mysql_query($sql);
		
		    $f = fopen('log1.txt', 'w');
    fwrite($f, $sql);
    fclose($f);

		

        
        while ($row = mysql_fetch_assoc($result)) {
            $result_array[] = $row;
        }
        echo json_encode($result_array);
        exit;
        
    } elseif ($_GET['text'] == 'drawPending') {
        //■操作が「保留データの表示」の場合
        
        $sql = "select * from expose where isPending = 1";  
        $result = mysql_query($sql);
		
		$countRow = mysql_num_rows($result); //件数
		


		
        while ($row = mysql_fetch_assoc($result)) {
            $result_array[] = $row;
        }
		echo json_encode($result_array);
		exit;
		
    } elseif ($_GET['text'] == 'cancelPending'){
    	//■操作が「保留解除」の場合：
		$pendingRowId = $_GET['pendingRowId'];
        $sql = "update expose set isPending = 0" . "  where id ='" . $pendingRowId . "'";
        $result = mysql_query($sql);
		
	$f = fopen('log1.txt', 'w');
    fwrite($f, "tarou = " . $sql);
    fclose($f);

    }

     
} //try
catch(Exception $e) {
    $f = fopen('log1.txt', 'a+');
    fwrite($f, $e->getMessage());
    fclose($f);
}


 function drawTodoTable(){
  
	    //■　メインテーブルの表示
	    $sql = "select * from expose ";
	    
	    $result = mysql_query($sql);
	    $countRow = mysql_num_rows($result); //件数
	    
	    while ($row = mysql_fetch_assoc($result)) {
	        $result_array[] = $row;
	    }
	
		$result_array[0]['count']= rand(1, $countRow);
		echo json_encode($result_array);
    }  
	
?>
